package com.deparse.dpframe.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.widget.Toast;

import com.deparse.dpframe.utils.DPLogUtil;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by Administrator on 2016/10/20.
 */
public class SaveImageAs extends AsyncTask {
    private String image;
    private Context context;

    public SaveImageAs(Context context, String image) {
        this.image = image;
        this.context = context;

    }

    private Bitmap getBitMBitmap(String urlpath) {

        Bitmap map = null;
        try {
            URL iconUrl = new URL(urlpath);
            URLConnection conn = iconUrl.openConnection();
            HttpURLConnection http = (HttpURLConnection) conn;

            int length = http.getContentLength();

            conn.connect();
            // 获得图像的字符流
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is, length);
            map = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();// 关闭流
            // TODO Auto-generated catch block
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }

    private void saveImageToGallery(Bitmap bmp) {
        // 首先保存图片
        File appDir = new File(Environment.getExternalStorageDirectory(), "V圈儿");
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        DPLogUtil.d("图片保存:" + "保存成功!");
        // 其次把文件插入到系统图库
//        try {
//            MediaStore.Images.Media.insertImage(context.getContentResolver(),
//                    file.getAbsolutePath(), fileName, null);
        DPLogUtil.d("图片保存:" + "图片路径:" + file.getAbsolutePath());
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
        // 最后通知图库更新
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getAbsolutePath())));
    }

    @Override
    protected Object doInBackground(Object[] params) {
        Bitmap b = getBitMBitmap(image);
        return b;
    }

    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
        if (o == null) {
            DPLogUtil.d("图片保存" + "Bitmap==null");
        } else {
            DPLogUtil.d("图片保存" + "Bitma!=null");
            saveImageToGallery((Bitmap) o);
            DPLogUtil.d("图片保存" + "success");
            Toast.makeText(context, "图片已保存到图库", Toast.LENGTH_SHORT).show();
        }
    }
}
